<?php


namespace control;
use Predis\Client;
use think\facade\Config;

class RedisClient
{
    private $redis;
    private $predis_is_open=false;
    private $string_time_out=3600;

    public function __construct()
    {
        $config = Config::get("redis");
        $this->predis_is_open=$config["predis_is_open"];
        $this->string_time_out=$config["string_time_out"];
        if($this->predis_is_open){
            $this->redis = new Client($config["predis"]["parameters"], $config["predis"]["options"]);
        }else{
            $this->redis = new \Redis();
            $this->redis->connect($config["redis"]["host"], $config["redis"]["port"]);
            $this->redis->auth($config["redis"]['password']);
            $this->redis->select($config["redis"]["select"]);
        }
    }

    /**
     * @param $key
     * @param $value
     * @return bool
     * 设置缓存
     */
    public function setValue($key, $value)
    {
        return $this->redis->set($key, $value);
    }

    /**
     * @param $key
     * @param $time
     * @param $val
     * @return bool
     * 设置缓存并附带过期时间
     */
    public function setex($key,$val,$time)
    {
        return $this->redis->setex($key, $time??$this->string_time_out, $val);
    }
    /**
     * @param $key
     * @return bool|string
     * 获取缓存
     */
    public function getValue($key)
    {
        return $this->redis->get($key);
    }

    /**
     * @param $key
     * 删除一个缓存key
     */
    public function del($key)
    {
        $this->redis->del($key);
    }

}